package com.app.twth.ui.b2bpick;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;

import com.app.twth.app.Constant;
import com.app.twth.base.BaseAndroidViewModel;
import com.app.twth.bean.BaseResponseEntity;
import com.app.twth.bean.LoadingBean;
import com.app.twth.net.JsonCallBack;
import com.app.twth.ui.b2cpick.B2CPickDetails;
import com.app.twth.ui.b2cpick.submit.B2CPickSubmit;
import com.app.twth.ui.b2cpick.submit.B2CPickSubmitQr;
import com.app.twth.ui.b2cpick.submit.SKUBarcodes;
import com.lzy.okgo.model.Response;

import java.util.List;


/**
 * ****************************************************************
 * 文件名称: LoginViewModel
 * 文件描述:
 * 注意事项:
 * 版权声明:
 * ****************************************************************
 */
public class B2BPickViewModel extends BaseAndroidViewModel<B2BPickDataModel> {

    public MutableLiveData<B2BPick> pickLiveData = new MutableLiveData<>();
    public MutableLiveData<B2BPick> pickItemLiveData = new MutableLiveData<>();
    public MutableLiveData<List<B2BPick>> pickListLiveData = new MutableLiveData<>();
    public MutableLiveData<List<B2CPickDetails>> pickDetailsLiveData = new MutableLiveData<>();
    public MutableLiveData<List<B2CPickDetails.SkuDetailListDTO>> skuDetailsLiveData = new MutableLiveData<>();

    public MutableLiveData<B2CPickSubmitQr> pickSubmitQrLiveData = new MutableLiveData<>();

    public MutableLiveData<SKUBarcodes> skuBarcodesLiveData = new MutableLiveData<>();
    public MutableLiveData<B2CPickSubmit> b2CPickSubmitLiveData = new MutableLiveData<>();

    public B2BPickViewModel(@NonNull Application application) {
        super(application);
    }

    public void b2bPickPendingJobs() {
        isLoading.setValue(new LoadingBean(true, Constant.loading));
        errorResultCode.setValue("");
        mModel.b2bPickPendingJobs(new JsonCallBack<BaseResponseEntity<List<B2BPick>>>() {
            @Override
            public void onSuccessData(Response<BaseResponseEntity<List<B2BPick>>> response) {
                isLoading.setValue(new LoadingBean(false, Constant.loading));
                BaseResponseEntity<List<B2BPick>> baseResponseEntity = response.body();
                if (baseResponseEntity == null) {
                    return;
                }
                if (baseResponseEntity.getCode() == null) {
                    return;
                }
                if (baseResponseEntity.getCode().equals("200")) {
                    pickListLiveData.setValue(baseResponseEntity.data);
                    return;
                }
                infoResultMsg = baseResponseEntity.getMessage();
            }

            @Override
            public void onError(String error) {
                isLoading.setValue(new LoadingBean(false, Constant.loading));
                infoResultMsg = error;
                errorResultCode.setValue(infoResultMsg);
            }
        });
    }

    public void b2bPickCheckPickNo(B2BPick b2BPick) {
        isLoading.setValue(new LoadingBean(true, Constant.loading));
        errorResultCode.setValue("");
        mModel.b2bPickCheckPickNo(b2BPick.getPickNo(), new JsonCallBack<BaseResponseEntity<List<B2CPickDetails>>>() {
            @Override
            public void onSuccessData(Response<BaseResponseEntity<List<B2CPickDetails>>> response) {
                isLoading.setValue(new LoadingBean(false, Constant.loading));
                BaseResponseEntity<List<B2CPickDetails>> baseResponseEntity = response.body();
                if (baseResponseEntity == null) {
                    return;
                }
                if (baseResponseEntity.getCode() == null) {
                    return;
                }
                if (baseResponseEntity.getCode().equals("200")) {
                    List<B2CPickDetails> data = baseResponseEntity.data;
                    if (data == null) {
                        return;
                    }
                    b2BPick.setDetailList(baseResponseEntity.data);
                    pickLiveData.setValue(b2BPick);
                    return;
                }
                infoResultMsg = baseResponseEntity.getMessage();
            }

            @Override
            public void onError(String error) {
                isLoading.setValue(new LoadingBean(false, Constant.loading));
                infoResultMsg = error;
                errorResultCode.setValue(infoResultMsg);
            }
        });
    }

    public void b2bPickCompleted(B2BPick b2BPick) {
        isLoading.setValue(new LoadingBean(true, Constant.loading));
        errorResultCode.setValue("");
        mModel.b2bPickCompleted(b2BPick.getPickNo(), new JsonCallBack<BaseResponseEntity<Object>>() {
            @Override
            public void onSuccessData(Response<BaseResponseEntity<Object>> response) {
                isLoading.setValue(new LoadingBean(false, Constant.loading));
                BaseResponseEntity<Object> baseResponseEntity = response.body();
                if (baseResponseEntity == null) {
                    return;
                }
                if (baseResponseEntity.getCode() == null) {
                    return;
                }
                if (baseResponseEntity.getCode().equals("200")) {
                    b2bPickPendingJobs();
                    return;
                }
                infoResultMsg = baseResponseEntity.getMessage();
            }

            @Override
            public void onError(String error) {
                isLoading.setValue(new LoadingBean(false, Constant.loading));
                infoResultMsg = error;
                errorResultCode.setValue(infoResultMsg);
            }
        });
    }

    public void b2bPickItems(B2BPick b2BPick) {
        isLoading.setValue(new LoadingBean(true, Constant.loading));
        errorResultCode.setValue("");
        mModel.b2bPickItems(b2BPick.getPickNo(), new JsonCallBack<BaseResponseEntity<List<B2BPick.Details>>>() {
            @Override
            public void onSuccessData(Response<BaseResponseEntity<List<B2BPick.Details>>> response) {
                isLoading.setValue(new LoadingBean(false, Constant.loading));
                BaseResponseEntity<List<B2BPick.Details>> baseResponseEntity = response.body();
                if (baseResponseEntity == null) {
                    return;
                }
                if (baseResponseEntity.getCode() == null) {
                    return;
                }
                if (baseResponseEntity.getCode().equals("200")) {
                    List<B2BPick.Details> data = baseResponseEntity.data;
                    if (data == null) {
                        return;
                    }
                    b2BPick.setList(baseResponseEntity.data);
                    pickItemLiveData.setValue(b2BPick);
                    return;
                }
                infoResultMsg = baseResponseEntity.getMessage();
            }

            @Override
            public void onError(String error) {
                isLoading.setValue(new LoadingBean(false, Constant.loading));
                infoResultMsg = error;
                errorResultCode.setValue(infoResultMsg);
            }
        });
    }

    public void b2bPickCheckIbLpn(String ibLpn) {
        isLoading.setValue(new LoadingBean(true, Constant.load_check));
        errorResultCode.setValue("");
        mModel.b2bPickCheckIbLpn(ibLpn, pickLiveData.getValue().getPickNo(), new JsonCallBack<BaseResponseEntity<List<B2CPickDetails.SkuDetailListDTO>>>() {
            @Override
            public void onSuccessData(Response<BaseResponseEntity<List<B2CPickDetails.SkuDetailListDTO>>> response) {
                isLoading.setValue(new LoadingBean(false, Constant.loading));
                BaseResponseEntity<List<B2CPickDetails.SkuDetailListDTO>> baseResponseEntity = response.body();
                if (baseResponseEntity == null) {
                    return;
                }
                if (baseResponseEntity.getCode() == null) {
                    return;
                }
                if (baseResponseEntity.getCode().equals("200")) {
                    pickSubmitQrLiveData.setValue(new B2CPickSubmitQr());
                    pickSubmitQrLiveData.getValue().setIbLpn(ibLpn);
                    pickSubmitQrLiveData.getValue().setPickNo(pickLiveData.getValue().getPickNo());

                    skuDetailsLiveData.setValue(baseResponseEntity.data);
                    return;
                }
                infoResultMsg = baseResponseEntity.getMessage();
            }

            @Override
            public void onError(String error) {
                isLoading.setValue(new LoadingBean(false, Constant.loading));
                infoResultMsg = error;
                errorResultCode.setValue(infoResultMsg);
            }
        });
    }

    public void b2bPickSkuInventoryDetail(int one, int two, String sku) {
        isLoading.setValue(new LoadingBean(true, Constant.loading));
        errorResultCode.setValue("");
        mModel.b2bPickSkuInventoryDetail(pickLiveData.getValue().getPickNo(), sku, new JsonCallBack<BaseResponseEntity<List<B2CPickDetails.LocationLpnDTO>>>() {
            @Override
            public void onSuccessData(Response<BaseResponseEntity<List<B2CPickDetails.LocationLpnDTO>>> response) {
                isLoading.setValue(new LoadingBean(false, Constant.loading));
                BaseResponseEntity<List<B2CPickDetails.LocationLpnDTO>> baseResponseEntity = response.body();
                if (baseResponseEntity == null) {
                    return;
                }
                if (baseResponseEntity.getCode() == null) {
                    return;
                }
                if (baseResponseEntity.getCode().equals("200")) {
                    B2CPickDetails tempPickUpBean = pickLiveData.getValue().getDetailList().get(one);
                    B2CPickDetails.SkuDetailListDTO pickSkuDetails = tempPickUpBean.getSkuDetailList().get(two);
                    pickSkuDetails.setLocationLpnDTOS(baseResponseEntity.data);
                    pickSkuDetails.setClick(true);
                    pickLiveData.setValue(pickLiveData.getValue());
                    return;
                }
                infoResultMsg = baseResponseEntity.getMessage();
            }

            @Override
            public void onError(String error) {
                isLoading.setValue(new LoadingBean(false, Constant.loading));
                infoResultMsg = error;
                errorResultCode.setValue(infoResultMsg);
            }
        });
    }

    public void getInfoByBarcode(String sku) {
        isLoading.setValue(new LoadingBean(true, Constant.loading));
        errorResultCode.setValue("");
        mModel.getInfoByBarcode(sku, new JsonCallBack<BaseResponseEntity<SKUBarcodes>>() {
            @Override
            public void onSuccessData(Response<BaseResponseEntity<SKUBarcodes>> response) {
                isLoading.setValue(new LoadingBean(false, Constant.loading));
                BaseResponseEntity<SKUBarcodes> baseResponseEntity = response.body();
                if (baseResponseEntity == null) {
                    return;
                }
                if (baseResponseEntity.getCode() == null) {
                    return;
                }
                if (baseResponseEntity.getCode().equals("200")) {
                    skuBarcodesLiveData.setValue(baseResponseEntity.data);
                    return;
                }
                infoResultMsg = baseResponseEntity.getMessage();
            }

            @Override
            public void onError(String error) {
                isLoading.setValue(new LoadingBean(false, Constant.loading));
                infoResultMsg = error;
                errorResultCode.setValue(infoResultMsg);
            }
        });
    }

    public void b2cPickSubmit() {
        isLoading.setValue(new LoadingBean(true, Constant.load_submission));
        errorResultCode.setValue("");
        mModel.b2bPickSubmit(pickSubmitQrLiveData.getValue(), new JsonCallBack<BaseResponseEntity<B2CPickSubmit>>() {
            @Override
            public void onSuccessData(Response<BaseResponseEntity<B2CPickSubmit>> response) {
                isLoading.setValue(new LoadingBean(false, Constant.loading));
                BaseResponseEntity<B2CPickSubmit> baseResponseEntity = response.body();
                if (baseResponseEntity == null) {
                    return;
                }
                if (baseResponseEntity.getCode() == null) {
                    return;
                }
                if (baseResponseEntity.getCode().equals("200")) {
                    B2CPickSubmit data = baseResponseEntity.data;

                    if (data.getPickSkuDetailList() != null && data.getPickSkuDetailList().size() > 0) {
                        skuDetailsLiveData.setValue(data.getPickSkuDetailList());
                        return;
                    }
                    b2CPickSubmitLiveData.setValue(baseResponseEntity.data);
                    return;
                }
                infoResultMsg = baseResponseEntity.getMessage();
            }

            @Override
            public void onError(String error) {
                isLoading.setValue(new LoadingBean(false, Constant.loading));
                infoResultMsg = error;
                errorResultCode.setValue(infoResultMsg);
            }
        });
    }
}
